﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;

using Ample.Model;

namespace Ample.DAL
{
    public class ProductManager : DBBase
    {
        public static ProductManager Create()
        {
            return new ProductManager();
        }

        #region Select Methods

        public IEnumerable<Product> GetProducts()
        {
            using (AmpleDataContext context = DataContext)
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Product>(p => p.ProductType);
                context.LoadOptions = dlo;
                return context.Ample_Products_SelectManual().ToArray<Product>();
                
            }
        }

        public Product GetProductById(int prodId)
        {
            using (AmpleDataContext context = DataContext)
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Product>(p => p.ProductType);
                context.LoadOptions = dlo;
                return context.Ample_Products_Select_By_ProductIdManual(prodId).SingleOrDefault<Product>();
            }
        }

        public IEnumerable<Product> GetProductByTypeId(int prodTypeId)
        {
            using (AmpleDataContext context = DataContext)
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Product>(p => p.ProductType);
                context.LoadOptions = dlo;
                return context.Ample_Products_Select_By_ProductTypeIdManual(prodTypeId).ToArray<Product>();
            }
        }

        #endregion

        #region Update Methods

        public OperationStatus UpdateProduct(Product prod)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.Ample_Products_Update(
                   prod.ProductId,
                   prod.ProductTypeId,
                   prod.Name,
                   prod.Price,
                   prod.Description,
                   prod.Package
                   );
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }


        #endregion

        #region Insert Methods

        public OperationStatus InsertProduct(Product prod)
        {
            AmpleDataContext context = null;
            int newprodId = -1;
            try
            {
                context = DataContext;
                newprodId = context.Ample_Products_Insert(
                   prod.ProductTypeId,
                   prod.Name,
                   prod.Price,
                   prod.Description,
                   prod.Package
                   );
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null, newprodId);
        }

        #endregion

        #region Delete Methods

        public OperationStatus DeleteProduct(Product prod)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.Ample_Products_Delete(prod.ProductId);
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }

        #endregion

    }
}
